package com.example.healkep.fragment.weight;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import androidx.fragment.app.Fragment;

import com.example.healkep.R;
import com.example.healkep.echarts.EchartOptionUtil;
import com.example.healkep.echarts.EchartView;
import com.example.healkep.fragment.ConnectNulls;

public class FragmentViewWeight extends Fragment {

    private double[] weight_old = new double[43];
    private Double[] weight = new Double[43];
    private EchartView lineChart;

    @Override
    public View onCreateView(final LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {

        final Intent intent = getActivity().getIntent();
        weight_old = intent.getDoubleArrayExtra("weight");

        for(int i=1; i<43; i++)
        {
            if(weight_old[i] == 0)  weight[i] = null;
            else weight[i] = weight_old[i];
        }
        ConnectNulls.Transform_nums(weight);

        final View view = inflater.inflate(R.layout.weight_fragment_view,
                container, false);
        initView(view);
        return view;
    }

    private void initView(View view) {
        lineChart = view.findViewById(R.id.lineChart);
        lineChart.setWebViewClient(new WebViewClient(){
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                //最好在h5页面加载完毕后再加载数据，防止html的标签还未加载完成，不能正常显示
                refreshLineChart();
            }
        });
    }

    private void refreshLineChart(){
        Object[] x = new Object[]{
                "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
                "11", "12", "13", "14", "15", "16", "17", "18", "19", "20",
                "21", "22", "23", "24", "25", "26", "27", "28", "29", "30",
                "31", "32", "33", "34", "35", "36", "37", "38", "39", "40","41", "42"
        };
        Object[] y = new Object[]{
                weight[1], weight[2], weight[3], weight[4], weight[5], weight[6], weight[7], weight[8], weight[9], weight[10],
                weight[11], weight[12], weight[13], weight[14], weight[15], weight[16], weight[17], weight[18], weight[19], weight[20],
                weight[21], weight[22], weight[23], weight[24], weight[25], weight[26], weight[27], weight[28], weight[29], weight[30],
                weight[31], weight[32], weight[33], weight[34], weight[35], weight[36], weight[37], weight[38], weight[39], weight[40],
                weight[41],weight[42]
        };
        lineChart.refreshEchartsWithOption(EchartOptionUtil.getLineChartOptionsW(x, y));
    }
}
